/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.compomics.ms1_intensitiesretrieval;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.filter.ElementFilter;
import org.jdom2.filter.Filter;
import org.jdom2.input.SAXBuilder;

/**
 *
 * @author Jonas
 */
public class FeatureXMLReader {

    private static final org.apache.log4j.Logger LOGGER = org.apache.log4j.Logger.getLogger(FeatureXMLReader.class);
    
    public List parseFeatureXML(File featureXMLfile) {

        double featureRTSec = 0.0;
        double featureMZ = 0.0;
        double featureIntensity = 0.0;
        double featureLowerTimeWindow = 0.0;
        double featureUpperTimeWindow = 0.0;
        
        ArrayList<FeatureVariables> featuresList = new ArrayList<FeatureVariables>();

        try {
            // Make use of the SAXParser
            SAXBuilder sAXBuilder = new SAXBuilder();
            // Create variable of type document and set this variable to contain the featureXMLfile
            Document document = sAXBuilder.build(featureXMLfile);
            // Get the rootelement out of the featureXMLfile
            Element rootElement = document.getRootElement();
            // Get the featurelist node, so you can look for "feature" children of this node
            Element featureListElement = rootElement.getChild("featureList");

            // Filter the selected data out of the featureXMLfile
            Filter featureFilter = new ElementFilter("feature");
            // Get all the descendants who are a "feature" node
            Iterator<Element> featureIterator = featureListElement.getDescendants(featureFilter);
            // Loop over the feature-nodes in the XML file
            while (featureIterator.hasNext()) {
                Element featureElement = featureIterator.next();
                List<Element> children = featureElement.getChildren();
                featureRTSec = Double.parseDouble(children.get(0).getValue());
                featureMZ = Double.parseDouble(children.get(1).getValue());
                featureIntensity = Double.parseDouble(children.get(2).getValue());

                // Filter out the convexhulls
                Filter convexhullFilter = new ElementFilter("convexhull");
                Iterator<Element> convexhullIterator = featureElement.getDescendants(convexhullFilter);
                // Loop over the convexhull elements for this feature
                while (convexhullIterator.hasNext()) {
                    Element convexhullElement = convexhullIterator.next();
                    if (convexhullElement.getAttributeValue("nr").equalsIgnoreCase("0")) {
                        List<Element> convexhullChildren = convexhullElement.getChildren();
                        // Lower value of the timewindow
                        featureLowerTimeWindow = Double.parseDouble(convexhullChildren.get(0).getAttributeValue("x"));
                        // Upper value of the timewindow
                        featureUpperTimeWindow = Double.parseDouble(convexhullChildren.get(1).getAttributeValue("x"));
                    }
                }
                FeatureVariables currentFeature = new FeatureVariables(featureRTSec, featureMZ, featureIntensity, featureLowerTimeWindow, featureUpperTimeWindow);
                featuresList.add(currentFeature);
            }

        } catch (JDOMException ex) {
            LOGGER.error(ex.getMessage());
        } catch (IOException ex) {
            LOGGER.error(ex.getMessage());
        }
        LOGGER.info("FeatureXMLFile read!");
        return featuresList;
    }
}
